<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
   /*  const data = {
      a: 10,
      b: 20
    }
    var a = 10;
    // Object.defineProperty 劫持的是对象的属性
    Object.defineProperty(data, 'a', {
      // getter 劫持的是 该属性 取值的操作
      get(){
        console.log('getter触发');
        return a;
      },
      // setter 劫持 该属性 赋值操作
      set(val){
        console.log('setter触发', val);
        a = val;
      }
    })
    console.log(data.a);
    data.a = 5000;
    console.log(data.a); */

    const data = {
      a: 10,
      b: 20
    }
    // Proxy劫持的整个对象  所有的属性都会被劫持
    const proxyData = new Proxy(data, {
      get(target, key){
        console.log('getter触发');
        return target[key]
      },
      set(target, key,val) {
        console.log('setter触发');
        target[key] = val;
      }
    })

    console.log(proxyData.a);
    proxyData.a = 1000;
    console.log(proxyData.a);
  </script>
</body>
</html>